package com.schedule.exception;

import com.schedule.pojo.RespBean;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 *
 * @author star
 * @since 1.0.0
 */
@RestControllerAdvice
public class GlobalException {
    @ExceptionHandler(SQLException.class)
    public RespBean mySqlException(SQLException e){
        if(e instanceof SQLIntegrityConstraintViolationException){
            System.out.println(e.getMessage());
            return RespBean.error("该数据有关联数据，操作失败！");
        }
        System.out.println(e.getMessage());
        return RespBean.error("数据库异常，操作失败！");
    }
    @ExceptionHandler(UsernameNotFoundException.class)
    public RespBean usernameNotFoundException(UsernameNotFoundException e){
        System.out.println(e.getMessage());
        return RespBean.error("用户不存在！！");
    }
}
